package tbs.framework.sql.ef.impl;

import tbs.framework.base.SFunction;
import tbs.framework.sql.ef.IQueryOrderProvider;
import tbs.framework.sql.ef.IQueryQueryBuilder;
import tbs.framework.sql.enums.QueryOrderEnum;
import tbs.framework.sql.model.SqlOrderInfo;
import tbs.framework.sql.utils.SqlUtils;
import tbs.framework.utils.LambdaUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * 排序功能提供器
 *
 * @author abstergo
 */
public class EntityQueryOrderProvider<T> implements IQueryOrderProvider<T> {

    List<SqlOrderInfo> orderInfos = new LinkedList<>();

    IQueryQueryBuilder<T> queryBuilder;

    public EntityQueryOrderProvider(IQueryQueryBuilder<T> queryBuilder) {
        this.queryBuilder = queryBuilder;
    }

    @Override
    public <P> IQueryOrderProvider<T> orderBy(SFunction<T, P> accessor, QueryOrderEnum order) {
        SqlOrderInfo info = new SqlOrderInfo();
        info.setOrder(order);
        info.setFiledName(SqlUtils.getFiledNameWithMapping(LambdaUtils.getField(accessor)));
        orderInfos.add(info);
        return this;
    }

    @Override
    public List<SqlOrderInfo> getSqlOrders() {
        return orderInfos;
    }

    @Override
    public IQueryQueryBuilder<T> pour() {
        return queryBuilder;
    }
}
